package com.yzt.message.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.yzt.zhuju.arouter.RouterUser;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.event.RxBus;
import com.yzt.zhuju.event.message.QuitGroupEvent;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.message.GroupBaseInfo;
import com.yzt.zhuju.network.entity.message.GroupInfoBean;
import com.yzt.zhuju.network.entity.message.GroupUserInfo;
import com.yzt.zhuju.widget.tuiview.TUIImageView;
import com.yzt.zhuju.widget.tuiview.TUITextView;
import com.yzt.message.R;
import com.yzt.message.adapter.GroupInfoUserAdapter;

import java.util.List;

import io.rong.imlib.ChannelClient;
import io.rong.imlib.IRongCoreCallback;
import io.rong.imlib.IRongCoreEnum;
import io.rong.imlib.model.Conversation;

/**
 * 作者：bobwang
 * 创建时间：2023-11-19 23:24:37
 * 描述：群聊信息
 */
@SuppressLint("SetTextI18n")
public class GroupInfoActivity extends BaseActivity {

    public final static int SPANCOUNT = 6;

    private GroupInfoUserAdapter mAdapter;

    private TextView tv_group_name;
    private TUIImageView img_status;
    private TUITextView tvQuit;
    private TextView tv_name;

    private String groupId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_group_info;
    }

    @Override
    public void installViews() {

        groupId = getIntent().getStringExtra("extra_id");

        tv_name = findViewById(R.id.tv_name);
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        tv_group_name = findViewById(R.id.tv_group_name);
        img_status = findViewById(R.id.img_status);
        tvQuit = findViewById(R.id.tv_quit);

        recyclerView.setLayoutManager(new GridLayoutManager(mActivity, SPANCOUNT));
        mAdapter = new GroupInfoUserAdapter();
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                GroupUserInfo bean = mAdapter.getItem(position);
                if (bean.getType() == 0) {
                    RouterUser.PersonalDetailActivity(bean.getUserId(), "");
                } else {
                    GroupCreateActivity.startSelf(GroupInfoActivity.this, bean.getType(), groupId);
                }
            }
        });

        img_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Conversation.ConversationType conversationType = Conversation.ConversationType.GROUP;
                String targetId = groupId;
                BizLayer.getInstance().getMessageModule().groupMessageMute(groupId, img_status.isSelected(), new AresSubscriber<Object>() {
                    @Override
                    protected void onSuccess(Object bean) {
                        img_status.setSelected(!img_status.isSelected());
                        if (img_status.isSelected()) {
                            ChannelClient.getInstance().setConversationNotificationLevel(conversationType, targetId,
                                    IRongCoreEnum.PushNotificationLevel.PUSH_NOTIFICATION_LEVEL_BLOCKED,
                                    new IRongCoreCallback.OperationCallback() {
                                        @Override
                                        public void onSuccess() {

                                        }

                                        @Override
                                        public void onError(IRongCoreEnum.CoreErrorCode coreErrorCode) {

                                        }
                                    });
                        } else {
                            ChannelClient.getInstance().setConversationNotificationLevel(conversationType, targetId,
                                    IRongCoreEnum.PushNotificationLevel.PUSH_NOTIFICATION_LEVEL_DEFAULT,
                                    new IRongCoreCallback.OperationCallback() {
                                        @Override
                                        public void onSuccess() {

                                        }

                                        @Override
                                        public void onError(IRongCoreEnum.CoreErrorCode coreErrorCode) {

                                        }
                                    });
                        }
                    }

                    @Override
                    protected void onError(int code, String info) {
                        super.onError(code, info);
                    }

                    @Override
                    protected void onFinallyEnd() {
                        super.onFinallyEnd();
                    }
                });


            }
        });

        tvQuit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BizLayer.getInstance().getMessageModule().groupQuit(groupId, new AresSubscriber<Object>() {
                    @Override
                    protected void onSuccess(Object bean) {
                        RxBus.getDefault().post(new QuitGroupEvent(groupId));
                        showToast("退出成功！");
                        finish();
                    }

                    @Override
                    protected void onError(int code, String info) {
                        super.onError(code, info);
                    }

                    @Override
                    protected void onFinallyEnd() {
                        super.onFinallyEnd();
                    }
                });
            }
        });
        tv_group_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (groupInfoBean != null && groupInfoBean.isCreated()) {
                    GroupEditNameActivity.startSelf(GroupInfoActivity.this, tv_group_name.getText().toString(), groupId);
                }
            }
        });

        findViewById(R.id.toolbar_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        getPageData();
    }

    private GroupInfoBean groupInfoBean;

    private void getPageData() {
        BizLayer.getInstance().getMessageModule().groupChatInfo(groupId, new AresSubscriber<GroupInfoBean>() {
            @Override
            protected void onSuccess(GroupInfoBean bean) {
                groupInfoBean = bean;
                tv_name.setText(bean.getGroupName() + "(" + bean.getMemberNum() + ")");
                tv_group_name.setText(bean.getGroupName());
                img_status.setSelected(bean.isMuted());
                BizLayer.getInstance().getMessageModule().groupChatUserList(groupId, new AresSubscriber<GroupBaseInfo>() {
                    @Override
                    protected void onSuccess(GroupBaseInfo bean1) {
                        mAdapter.getData().clear();
                        List<GroupUserInfo> memberList = bean1.getMemberList();
                        memberList.add(new GroupUserInfo("-1", -1));
                        if (bean.isCreated()) {
                            memberList.add(new GroupUserInfo("-2", -2));
                        }
                        mAdapter.addData(memberList);
                    }

                    @Override
                    protected void onError(int code, String info) {
                        super.onError(code, info);
                    }

                    @Override
                    protected void onFinallyEnd() {
                        super.onFinallyEnd();
                    }
                });
            }

            @Override
            protected void onError(int code, String info) {
                super.onError(code, info);
            }

            @Override
            protected void onFinallyEnd() {
                super.onFinallyEnd();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getPageData();
    }
}
